<template>
  <div class="xtx-goods-page" v-if="goods.categories">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread separator="/">
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goods.categories[1].id}`">
          {{ goods.categories[1].name }}
        </XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goods.categories[0].id}`">
          {{ goods.categories[0].name }}
        </XtxBreadItem>
        <XtxBreadItem to="/">
          {{ goods.name }}
        </XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 图片预览区 -->
        <div class="media">
          <GoodsImage :imageList="goods.mainPictures" />
          <!-- 销量组件 -->
          <GoodsSales :goods="goods" />
          <!-- 测试 -->
          <XtxCheckbox v-model="sel" v-model:meng="goods">
            黑马程序员
          </XtxCheckbox>
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <!-- 信息组件 -->
          <GoodsName :goods="goods" />
          <!-- sku -->
          <GoodsSku :goods="goods" @change="getSku" />
          <!-- 库存 -->
          <XtxNumbox
            v-model="buyNum"
            v-model:test="test"
            meng="1"
            :max="goods.inventory"
          />
          <!-- <button @click="buyNum++">add</button> -->
          <!-- 购买 -->
          <XtxButton
            @click="insertCart"
            style="margin-top: 20px"
            type="primary"
          >
            加入购物车
          </XtxButton>
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsTabs />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1" />
          <GoodsHot :type="2" />
          <GoodsHot :type="3" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { findGoods } from '@/api/goods'
import { ref, onMounted, provide } from 'vue'
import { useRoute } from 'vue-router'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales'
import GoodsName from './components/goods-name'
import GoodsTabs from './components/goods-tabs'
import GoodsHot from './components/goods-hot'
import Message from '@/components/Message'
import { useStore } from 'vuex'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsImage, GoodsSales, GoodsName, GoodsTabs, GoodsHot
  },
  setup () {
    const route = useRoute()
    const goods = ref({})
    const buyNum = ref(18)
    const test = ref(100)

    // 测试
    const sel = ref(false)
    // 注入goods详情数据
    provide('goods', goods)
    async function getGoods () {
      const res = await findGoods(route.params.id)
      console.log('商品详情数据：', res)
      goods.value = res.result
    }
    onMounted(() => {
      getGoods()
    })

    // 获取sku数据
    const getSku = (sku) => {
      console.log('sku:', sku)
      // 修改商品的现价原价库存信息
      if (sku.skuId) {
        goods.value.price = sku.price
        goods.value.oldPrice = sku.oldPrice
        goods.value.inventory = sku.inventory
        // 存储sku信息
        currSku.value = sku
      } else {
        currSku.value = null
      }
    }

    // 加入购物车
    const currSku = ref(null)
    const store = useStore()
    const insertCart = async () => {
      console.log(111111, await store.dispatch('cart/isLogin'))
      // 没有完整的sku数据 直接return
      if (!currSku.value) {
        return Message({ text: '请选择商品规格' })
      }
      // 库存处理
      if (!currSku.value.inventory) {
        return Message({ text: '库存不足' })
      }
      // 购买数量大于库存 直接return
      // if (buyNum.value > goods.value.inventory) {
      //   return Message({ text: '库存不足' })
      // }
      store.dispatch('cart/asyncInsertCart', {
        id: goods.value.id,
        name: goods.value.name,
        picture: goods.value.mainPictures[0],
        skuId: currSku.value.skuId,
        price: currSku.value.oldPrice,
        nowPrice: currSku.value.price,
        attrsText: currSku.value.specsText,
        stock: currSku.value.inventory,
        selected: true,
        isEffective: true,
        count: buyNum.value
      }).then((r) => {
        console.log('add:', r)
        Message({ type: 'success', text: '加入购物车成功' })
      })
    }

    return { goods, getSku, buyNum, sel, insertCart, test }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    background: #fff;
    height: 100%;
    // min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
